<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="硬件代码" prop="hardwareCode">
        <el-input v-model="queryParams.hardwareCode" placeholder="请输入硬件代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="硬件名称" prop="hardwareName">
        <el-input v-model="queryParams.hardwareName" placeholder="请输入硬件名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="硬件版本" prop="hardwareVersion">
        <el-input v-model="queryParams.hardwareVersion" placeholder="请输入硬件版本" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="PCBA代码" prop="pcbaCode" label-width="80px">
        <el-input v-model="queryParams.pcbaCode" placeholder="请输入PCBA代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="关键料" prop="keyMaterial">
        <el-input v-model="queryParams.keyMaterial" placeholder="请输入关键料" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['produce:product-hardware-info:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['produce:product-hardware-info:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['produce:product-hardware-info:removed']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['produce:product-hardware-info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProductHardwareInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="硬件代码" align="center" prop="hardwareCode" />
      <el-table-column label="硬件名称" align="center" prop="hardwareName" />
      <el-table-column label="硬件版本" align="center" prop="hardwareVersion" />
      <el-table-column label="PCBA代码" align="center" prop="pcbaCode" />
      <!-- <el-table-column label="BOM清单" align="center" prop="bomList" /> -->
      <el-table-column label="BOM清单" align="center" prop="bomList" >
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.bomList.split(',')" :key="index">
              <el-button size="mini" style="font-size: 15px;" type="text" @click="handleDownload(item)"> 
                  链接{{ index+1 }}
              </el-button>
            </div>
              
          </template>
      </el-table-column>
      <el-table-column label="关键料" align="center" prop="keyMaterial" />
      <el-table-column label="存在的BUG" align="center" prop="existBug" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:product-hardware-info:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['produce:product-hardware-info:removed']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品硬件详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="产品类型" prop="productType">
            <el-select v-model="form.productType" placeholder="产品类型" @change="getProductInfoList($event)" clearable filterable>
                <el-option v-for="item in productTypeOption" :key="item.code" :label="item.name"
                    :value="item.code" />
            </el-select>
        </el-form-item>
        <el-form-item label="关联产品" prop="productCodes">
          <el-select v-model="form.productCodes" placeholder="关联产品" clearable filterable multiple>
            <el-option v-for="item in productInfoOption" :key="item.productCode" :label="item.productCode +'-'+ item.productName"
              :value="item.productCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="硬件代码" prop="hardwareCode">
          <el-input v-model="form.hardwareCode" placeholder="请输入硬件代码" type="text" maxlength="3" show-word-limit/>
        </el-form-item>
        <el-form-item label="硬件名称" prop="hardwareName">
          <el-input v-model="form.hardwareName" placeholder="请输入硬件名称" />
        </el-form-item>
        <el-form-item label="硬件版本" prop="hardwareVersion">
          <el-input v-model="form.hardwareVersion" placeholder="请输入硬件版本" />
        </el-form-item>
        <el-form-item label="PCBA代码" prop="pcbaCode">
          <el-input v-model="form.pcbaCode" placeholder="请输入PCBA代码" />
        </el-form-item>
        <el-form-item label="BOM清单" prop="bomList">
          <file-upload-oss v-model="form.bomList" :fileTypeCode="1" placeholder="请输入BOM清单" />
        </el-form-item>
        <el-form-item label="关键料" prop="keyMaterial">
          <el-input v-model="form.keyMaterial" placeholder="请输入关键料" />
        </el-form-item>
        <el-form-item label="存在的BUG" prop="existBug">
          <el-input type="textarea" :rows="2" v-model="form.existBug" placeholder="请输入存在的BUG" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { listProductHardwareInfo, getProductHardwareInfo, removedProductHardwareInfo, addProductHardwareInfo, updateProductHardwareInfo } from "@/api/produce/product-hardware-info";
import { getAll } from "@/api/produce/product-software-info";
import { listProductType } from "@/api/produce/product-type";
import { listProductInfo } from "@/api/produce/product-info";

export default {
  name: "Product-hardware-info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品硬件详情表格数据
      ProductHardwareInfoList: [],
      // 产品软件件详情表格数据
      productSoftwareInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hardwareCode: null,
        hardwareName: null,
        hardwareVersion: null,
        pcbaCode: null,
        bomList: null,
        keyMaterial: null,
        existBug: null,
        removed: null,
        orderByColumn:'id',
        isAsc:'desc'
      },
      // 产品类型查询
      productTypeQueryParams: {
        pageNum: 1,
        pageSize: 100,
      },
      // 产品类型选项
      productTypeOption: [],
      // 产品选项
      productInfoOption: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productType: [
          { required: true, message: "产品类型不能为空", trigger: "blur" }
        ],
        hardwareCode: [
          { required: true, message: "硬件代码不能为空", trigger: "blur" },
          { required: true, message: "输入规则：大小写字母、数字", trigger: "blur", pattern: /^[a-zA-Z0-9]+$/ }
        ],
        hardwareName: [
          { required: true, message: "硬件名称不能为空", trigger: "blur" }
        ],
        hardwareVersion: [
          { required: true, message: "硬件版本不能为空", trigger: "blur" }
        ],
        pcbaCode: [
          { required: true, message: "PCBA代码不能为空", trigger: "blur" }
        ],
        bomList: [
          { required: true, message: "BOM清单不能为空", trigger: "blur" }
        ],
        keyMaterial: [
          { required: true, message: "关键料不能为空", trigger: "blur" }
        ],
        productCodes: [
          { required: true, message: "关联产品不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** BOM清单下载 */
    handleDownload(row){
      // console.log(row.bomList)
      let url = row
      const a = document.createElement('a')
      a.href = url
      a.download = row.bomList // 下载后文件名
      a.style.display = 'none'
      document.body.appendChild(a)
      a.click() // 点击下载
      document.body.removeChild(a) // 下载完成移除元素
      // window.location.href = row.bomList
    },
    /** 查询产品下拉*/
    getProductInfoList(val) {
      // 每次下拉都先清空
      this.form.productCodes= [];
      let qurey = {
        pageNum: 1,
        pageSize: 999,
        productCode: null,
        productName: null,
        productTypeCode: val,
        removed: null
      };
      listProductInfo(qurey).then(response => {
        this.productInfoOption = response.rows;
        this.loading = false;
      });
    },
    /**查询产品类型下拉 */
    getProductType () {
      listProductType(this.productTypeQueryParams).then(response => {
        this.productTypeOption = response.rows;
        this.loading = false;
      });
    },
    /** 查询产品硬件详情列表 */
    getList() {
      this.loading = true;
      listProductHardwareInfo(this.queryParams).then(response => {
        this.ProductHardwareInfoList = response.rows;
        
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hardwareCode: null,
        hardwareName: null,
        hardwareVersion: null,
        pcbaCode: null,
        bomList: null,
        keyMaterial: null,
        existBug: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        removed: null,
        productType: null,
        productCodes: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getProductType();
      this.reset();
      this.open = true;
      this.title = "添加产品硬件详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getProductType ();
      const id = row.id || this.ids
      getProductHardwareInfo(id).then(response => {
        this.form = response.data;
        var productInfoItem = response.data.productInfos;
        // 判断关联产品是否存在
        if (productInfoItem != null){
          var list = [];
          productInfoItem.forEach(function(element) {
              list.push(element.productCode);
          });
          this.$set(this.form, "productCodes", list);
          this.$set(this.form, "productType", productInfoItem[0].productTypeCode);
        }
        this.open = true;
        this.title = "修改产品硬件详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductHardwareInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductHardwareInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品硬件详情编号为"' + ids + '"的数据项？').then(function () {
        // 修改为逻辑删除
        return removedProductHardwareInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('produce/product-hardware-info/export', {
        ...this.queryParams
      }, `product-hardware-info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
  